//
//  DynamicViewController.m
//  HomePage
//
//  Created by dlios on 15-1-28.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DynamicViewController.h"
#import "DynamicCell.h"
#import "DynamicListViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "Dynamic.h"
#import "UIImageView+WebCache.h"
@interface DynamicViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableview;
@property (nonatomic,retain)NSMutableArray *array;
@property (nonatomic,retain)UIScrollView *scroview;

@end

@implementation DynamicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"动态";
  
    [self getdata];
    
}

- (void)getdata
{
    self.array = [NSMutableArray array];
    NSString *str = @"http://mobile.ximalaya.com/mobile/api/1/feed/unlogin/dynamic?device=android";
    
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
     [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
         NSString *html = operation.responseString;
         NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
         NSError *error = nil;
         NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
         NSMutableArray *array = [NSMutableArray array];
         array = [dic objectForKey:@"data"];
         for (NSMutableDictionary *temp in array) {
             Dynamic *dy = [[Dynamic alloc]init];
             [dy setValuesForKeysWithDictionary:temp];
             [self.array addObject:dy];
         }
         NSLog(@"%ld",self.array.count);
         [self creatTableView];
         
         
         
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"%@",error);
     }];
    
}


- (void)creatTableView
{
    self.tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    _scroview = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 130)];
    _scroview.contentSize = CGSizeMake(self.view.frame.size.width * 4, 0);
    _scroview.backgroundColor = [UIColor yellowColor];
   // _scroview.pagingEnabled = YES;
    
    self.tableview.tableHeaderView = _scroview;
//    UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 120, self.view.frame.size.width, 10)];
//    page.backgroundColor = [UIColor blackColor];
//    page.numberOfPages = 4;
//    [page addTarget:self action:@selector(changevalue:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //[self.view addSubview:page];
    [self.view addSubview:self.tableview];
    
    
   // [page release];
    [_scroview release];
    [_tableview release];
}

- (void)changevalue:(UIPageControl *)page
{
    page.currentPage = (_scroview.contentOffset.x / self.view.frame.size.width);
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *str = @"nihao";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
    }
    
    Dynamic *dynamic = [self.array objectAtIndex:indexPath.row];
    
    cell.textLabel.text = dynamic.nickname;
    NSURL *url  = [NSURL URLWithString:dynamic.avatar];
    [cell.imageView setImageWithURL:url];
    cell.detailTextLabel.text = dynamic.personalSignature;
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DynamicListViewController *list = [[DynamicListViewController alloc]init];
    [self.navigationController pushViewController:list animated:YES];
    [list release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
